<?php
// +----------------------------------------------------------------------
// | 随访系统
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2050 http://www.hedesoft.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +---------------------------------------------------------------------
// | Author: 小李子 < 490959289@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;
use app\admin\controller\Base;
use think\Validate;
class Menu extends Base
{
    public function index()
    {
        $key = $this->request->param('key');
        $where = array();
        if ($key){
             $where['menu_name'] = array('LIKE', '%'.$key.'%');
        }else{
            $where['parent_id'] = 0;
        }
        $list=model('AdminMenu')->where($where)->order('menu_id desc')->paginate(5);
        // 获取分页显示
        $page = $list->render();
        foreach ($list as $key => $value) {
            // $parent_name=model('AdminMenu')->where('menu_id='.$value['parent_id'])->field('menu_name')->find();
            $list_n = model('AdminMenu')->where(['parent_id'=>$value['menu_id']])->order('menu_id','asc')->select();
            foreach ($list_n as $k => $val) {
                    $list_n[$k]['menu_name'] = "└ ".$val['menu_name'];
                    $list_n[$k]['parent_name'] = $value['menu_name'];
            }
            $list[$key]['next'] = $list_n;
            // if ($parent_name) {
            //     $list[$key]['parent_name'] = $parent_name['menu_name'];
            // }else{
            //     $list[$key]['parent_name'] = '顶层';
            // }
            
        }
        $this->assign('list', $list);
        $this->assign('page', $page);
    	return $this->fetch();
    }
    //加载添加/编辑页
    public function add_menu(){
    $menu_id = $this->request->param('menu_id');
    $menu = model('AdminMenu')->where('menu_id',$menu_id)->find();
    $this->assign('data', $menu);
    $list=model('AdminMenu')->where('parent_id',0)->select();
    $this->assign('list', $list);
    return $this->fetch();	
    }
    // public function icons()
    // {
    // 	return $this->fetch();
    // }
    //获取菜单数据保存
    public function getdata(){
        $rule = [
        'menu_name'  => 'require|max:255',
        // 'app'  => 'require|max:255',
        // 'icon'  => 'require|max:255',
        ];

        $msg = [
            'menu_name.require' => '菜单名不能为空',
            // 'app.require' => '模块名不能为空',
            // 'icon.require' => '请选择小图标',
        ];

        $cont = $this->request->param('cont_name');
        if (preg_match('/^[a-z]/', $cont)) {
           echo json_encode(['status'=>0,'msg'=>'控制器首字母必须大写']);exit();
        }
        $data['menu_name'] = $this->request->param('menu_name');
        $data['parent_id'] = $this->request->param('parent_menu');
        $data['menu_type'] = $this->request->param('menu_type');
        $data['menu_status'] = $this->request->param('radio2');
        $data['menu_sort'] = $this->request->param('menu_sort');
        $data['app'] = $this->request->param('model_name');
        $data['controller'] = $this->request->param('cont_name');
        $data['action'] = $this->request->param('action_name');
        $data['param'] = $this->request->param('paran');
        $data['icon'] = $this->request->param('icons');
        // $data['remark'] = $this->request->param('remark');
        $validate = new Validate($rule, $msg);
        if (!$validate->check($data)){
            echo json_encode(['status'=>0,'msg'=>$validate->getError()]);exit();
        }
        $menu_id = $this->request->param('menu_id');
        if ($menu_id){
            $menu_status = model('AdminMenu')->where('menu_id',$menu_id)->update($data);
        }else{
            $menu_status = model('AdminMenu')->insertGetId($data);
        }
        if ($menu_status){
            // $this->create_adminlog('保存 6666 >'.$data['menu_name'].' 菜单');
            echo json_encode(['status'=>1,'msg'=>'保存成功']);exit();
        }else{
            echo json_encode(['status'=>0,'msg'=>'保存失败']);exit();
        }
    }

    //删除菜单
    public function delMenu()
    {
        $data['menu_id'] = $this->request->param('menu_id');
        $where['parent_id'] = $this->request->param('menu_id');
        $menu_status = model('AdminMenu')->where($data)->delete();
        $menu_res = model('AdminMenu')->where($where)->delete();
        if ($menu_status){
            echo json_encode(['status'=>1,'msg'=>'删除成功']);exit();
        }else{
            echo json_encode(['status'=>0,'msg'=>'删除失败']);exit();
        }
    }
}
